<script setup>
import { ref } from 'vue'
import { artAddChannelService, artEditChannelService } from '@/api/article'
const form = ref({})
const refForm = ref()
const dialogVisible = ref(false)
const open = (obj) => {
  dialogVisible.value = true
  if(obj){
    form.value = obj
  }else{
    form.value = {
      cate_name: '',
      cate_alias: ''
    }
  }
}
const emit = defineEmits(['getArticleChannel'])
const rules = ref({
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' }
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' }
  ]
})
const onSubmit = async() => {
  await refForm.value.validate()
  if(form.value.id){
    const res = await artEditChannelService(form.value)
    console.log(res)
    if(res.status === 200){
      ElMessage.success('添加成功')
      dialogVisible.value = false
      emit('getArticleChannel')
    }else{
      ElMessage.error(res.data.message)
    }
  }else{
    const res = await artAddChannelService(form.value)
    console.log(res)
    if(res.status === 200){
      ElMessage.success('编辑成功')
      dialogVisible.value = false
      emit('getArticleChannel')
    }else{
      ElMessage.error(res.data.message)
    }
  }
}
defineExpose({
  open
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="form.cate_name ? '编辑分类' : '添加分类'"
    width="500"
  >
    <el-form :model="form" label-width="auto" style="max-width: 600px" :rules="rules" ref="refForm">
      <el-form-item label="分类名称" prop="cate_name" style="width: 90%; margin-left: 20px">
        <el-input v-model="form.cate_name" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="分类别名" prop="cate_alias" style="width: 90%; margin-left: 20px">
        <el-input v-model="form.cate_alias" placeholder="请输入分类别名"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="onSubmit">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
